@model WalkingTec.Mvvm.Core.IBasePagedListVM<WalkingTec.Mvvm.Core.TopBasePoco,WalkingTec.Mvvm.Core.BaseSearcher>
@using WalkingTec.Mvvm.Core.Extensions;
@using Newtonsoft.Json;
@{
    var tempvar = $"var_{Guid.NewGuid().ToNoSplitString()}";
}

  <script>
    function submitSelect() {
        var res = []
            ,multiSel = @(ViewBag.MultiSel.ToString().ToLower())
            ,displayStrArr = []
            ,hiddenStrArr = [];
        selected = @(tempvar);
        @if (ViewBag.SubmitFunc != null)
        {
            @(ViewBag.SubmitFunc.ToString())<Text>(selected);</Text>;
        }
        if (selected.length > 0) {
            if (!multiSel && selected.length > 1) {
                layui.layer.msg('@WalkingTec.Mvvm.Core.Program._localizer["SelectOneRowMax"]');
                return;
            }
            for (var i = 0; i < selected.length; i++) {
                displayStrArr.push(selected[i]['@ViewBag.TextName']);
                hiddenStrArr.push("<input type='hidden' wtm-tag='wtmselector' name='@ViewBag.FieldName' value='" + selected[i]['@ViewBag.ValName'] +"' />");
            }
        }

        ff.CloseDialog();

        var vals = $('#@(ViewBag.SelId)_Container input[type=hidden]');
        for (var i = 0; i < vals.length; i++) {
            vals[i].remove();
        }
        $("#@(ViewBag.SelId)_Display").val(displayStrArr.join(","));
        $("#@(ViewBag.SelId)_Display").after(hiddenStrArr.join(''));
    }


    var @(tempvar) = @Html.Raw(ViewBag.SelectData);
    function gridCheckedFunc(obj) {
        var ids = []
            ,gridId = 'wtTable_@Model.UniqueId';
        if (obj.checked === true) {  //选中
            if (obj.type === "all") { // 全选
                var selected = ff.GetSelectionData(gridId);
                for (var i = 0; i < selected.length; i++) {
                    var checked = false;
                    for (var j = 0; j < @(tempvar).length; j++) { // 是否已经选中
                        if (@(tempvar)[j].ID == selected[i].ID) {
                            checked=true;
                            break;
                        }
                    }
                    if(!checked)
                        @(tempvar).push(selected[i]);
                }
            } else {
                if (obj.data.ID === undefined) {
                    obj.data = ff.GetSelectionData(gridId).pop();
                }
                @(tempvar).push(obj.data);
            }
        } else {  //取消选中
            if (obj.type === "all") { // 取消全选
                var nonSelected = ff.GetNonSelectionData(gridId);
                //移除未选中数据
                for (var i = 0; i < nonSelected.length; i++) {
                    @(tempvar).removeByID(nonSelected[i]);
                }
            } else {
                @(tempvar).removeByID(obj.data);
            }
        }
        for (var i = 0; i < @(tempvar).length; i++) {
            ids.push(@(tempvar)[i].ID);
        }
        wtVar_@(Model.UniqueId).config.where.Ids = ids;
    }
  </script>

<wt:container style="margin:0px 10px 0px 10px" class='donotuse_pdiv'>
    <div style="height:10px">&nbsp;</div>
    $$SearchPanel$$
    <wt:grid vm="@Model" hidden-panel="true" checked-func="gridCheckedFunc" />
    <wt:row align="AlignEnum.Right">
        <wt:button click="submitSelect();" text="@WalkingTec.Mvvm.Core.Program._localizer["OK"]" size="ButtonSizeEnum.Normal" />
    </wt:row>
</wt:container>
